/*
 * Copyright (C) 2005 Dmitri Maximovich <maxim@md.pp.ru>
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

// Preferences object
// work in progress
function Prefs() {
	this.url = "";
	this.jmxUrl = "";
	this.refresh = 0;
	this.include = "";
	this.exclude = "";
	this.sortBy = 0;
	this.dateFormat = "";
	this.debug = false;
	this.onlyFailed = false;
	this.maxProjectNameLen = 25;
	this.lastUpdate = 0;

	this.load = function() {
		var p = getPrefService();
		this.url = p.getCharPref("url");
		this.jmxUrl = p.getCharPref("jmxUrl");
		this.refresh = p.getIntPref("refresh");
		this.include = p.getCharPref("include");
		this.exclude = p.getCharPref("exclude");
		this.sortBy = p.getIntPref("sortBy");
		this.dateFormat = p.getCharPref("dateFormat");
		this.debug = p.getBoolPref("debug");
		this.onlyFailed = p.getBoolPref("includeOnlyFailed");
        this.maxProjectNameLen = p.getIntPref("maxProjectNameLen");
	}

	this.save = function() {
		var p = getPrefService();
		p.setCharPref("url", this.url);
		p.setCharPref("jmxUrl", this.jmxUrl);
		p.setIntPref("refresh", this.refresh);
		p.setCharPref("include", this.include);
		p.setCharPref("exclude", this.exclude);
		p.setIntPref("sortBy", this.sortBy);
		p.setCharPref("dateFormat", this.dateFormat);
		p.setBoolPref("debug", this.debug);
		p.setBoolPref("includeOnlyFailed", this.onlyFailed);
	}

	this.toString = function() {
		return "Prefs[url="+this.url+", refresh="+this.refresh+", include="+this.include+", exclude="+this.exclude
		+", sortBy="+this.sortBy+", dateFormat="+this.dateFormat+", debug="+this.debug+", onlyFailed="+this.onlyFailed
		+", maxProjectNameLen="+this.maxProjectNameLen+", lastUpdate="+this.lastUpdate+"]";
	}
}

function getPrefService() {
	var prefs = Components.classes["@mozilla.org/preferences;1"].getService(Components.interfaces.nsIPrefService);
	return prefs.getBranch("cc.")
}


// preferences dialog methods
function ccLoadPreferences() {
	var p = new Prefs();
	p.load();
	document.getElementById('ccDialUrl').value = p.url;
	document.getElementById('ccDialJmxUrl').value = p.jmxUrl;
	document.getElementById('ccDialRefresh').value = p.refresh/1000;
	document.getElementById('ccDialInclude').value = p.include;
	document.getElementById('ccDialExclude').value = p.exclude;
	document.getElementById('ccDialDebugEnabled').checked = p.debug;
	document.getElementById('ccSortByMenu').selectedIndex = p.sortBy;
	document.getElementById('ccDialIncludeOnlyFailed').checked = p.onlyFailed;
	document.getElementById('ccDateFormat').value = p.dateFormat;
}

function ccSavePreferences() {
	var p = new Prefs();
	p.url = document.getElementById('ccDialUrl').value;
	p.jmxUrl = document.getElementById('ccDialJmxUrl').value;
	p.refresh = 1000 * document.getElementById('ccDialRefresh').value;
	p.include = document.getElementById('ccDialInclude').value;
	p.exclude = document.getElementById('ccDialExclude').value;
	p.sortBy = document.getElementById('ccSortByMenu').selectedIndex;
	p.dateFormat = document.getElementById('ccDateFormat').value;
	p.debug = document.getElementById('ccDialDebugEnabled').checked;
	p.onlyFailed = document.getElementById('ccDialIncludeOnlyFailed').checked;
	p.save();

	// NOTE: update forced by the fact we created new Prefs() here!
	
	// replace global prefs, in opener window
	opener.prefs = p;
	// force get data
	opener.getCcData();
}
